package nz.ac.massey.cs.se400_2013.ass3.message;

import java.util.ArrayList;

/**
 * @author Chen
 * <h1>MessageHandler</h1>
 * 
 * <p>
 * A handler to dispose message instead of message producer
 * </p>
 * 
 */
public class MessageHandler {
	private Message message;
	private ArrayList<MessageListener> listeners; // A list of message listeners.

	public MessageHandler() {
		this.listeners = new ArrayList<MessageListener>();
	}

	public void addListener(MessageListener listener) {
		listeners.add(listener);
	}

	public void removeListener(MessageListener listener) {
		listeners.remove(listener);
	}

	public void sendMessage(Message message) {
		this.message = message;
		notifyListeners();
	}

	private void notifyListeners() {
		for (MessageListener listener : listeners) {
			listener.messageReceived(message);
		}
	}
}
